<!--  -->
<template>
  <div style="padding-top:100px">
    <div class="app-title-container title-position">
      <modules-title :titleName="titleName">
        <template slot="btn">
          <el-button class="filter-item" plain="" size="small" @click="handleBack">返回
          </el-button>
          <!--          <el-button class="filter-item" plain="" icon="el-icon-refresh" size="small"-->
          <!--                     @click="handleReset">重置-->
          <!--          </el-button>-->
          <el-button class="filter-item" type="primary" size="small" @click="handleSubmit">提交审核
          </el-button>

        </template>
      </modules-title>
    </div>

    <div style="padding:20px">
      <el-form ref="form" :model="form" :rules="rules" label-width="100px" size="small">
        <el-row :gutter="80">
          <el-col :span="12">
            <el-form-item label="请验类型">
              <el-select v-model="form.inspection_type" style="width:100%" @change="handlChangeType"
                         placeholder="请选择请验类型">
                <el-option v-for="(item,index) in demandSourceOptions" :key="index" :label="item.dict_val"
                           :value="item.dict_key"></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="检验目的" prop="inspection_goal_id">
              <el-select v-model="form.inspection_goal_id" @change="handlGoal" style="width:100%" placeholder="请选择检验目的">
                <el-option v-for="(item,index) in testingPurpose" :key="index" :label="item.dict_val"
                           :value="item.dict_key"/>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="项目名称" prop="project_name">
              <el-select v-model="form.project_name" filterable placeholder="请输入项目名称(可搜索)"
                         @change="handlChange" clearable style="width:100%">
                <el-option v-for="item in projectData" :key="item.id"
                           :label="`${item.project_name}(${ item.project_phases_id === '1' ? '临床前研究' : '临床研究'})`"
                           :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="项目编号">
              <el-input v-model="form.project_no" disabled placeholder="请输入项目编号"/>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="检品名称" prop="sample_info.name">
              <!--              <el-autocomplete-->
              <!--                v-model.trim="form.sample_info.name" :fetch-suggestions="querySearchAsync"-->
              <!--                placeholder="请输入内容" @select="handlInspection" style="width:100%">-->
              <!--                <template slot-scope="{ item }">-->
              <!--                  <div class="product-item">-->
              <!--                    <div class="name">-->
              <!--                      <span>{{ item.name }}</span><span>({{ item.sample_type_name }})</span>-->
              <!--                    </div>-->
              <!--                  </div>-->
              <!--                </template>-->
              <!--              </el-autocomplete>-->
              <el-input v-model.trim="form.sample_info.name" placeholder="请输入内容"></el-input>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="批号" prop="sample_info.batch_number">
              <el-input v-model="form.sample_info.batch_number" placeholder="请输入批号"/>
            </el-form-item>
          </el-col>


          <el-col :span="12">
            <el-form-item label="样品类型">
              <el-select v-model="form.sample_info.sample_type_info.same_type_id" @change="handlSampType"
                         style="width: 100%" placeholder="请选择">
                <el-option v-for="item in sampleTypeList" :key="item.dict_key" :label="item.dict_val"
                           :value="item.dict_key"/>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="CAS">
              <el-input v-model="form.sample_info.cas" placeholder="请输入CAS"/>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="生产日期">
              <el-date-picker v-model="form.sample_info.production_date" type="date"
                              :picker-options="pickerOption" value-format="yyyy-MM-dd" @change="onChangeTime1"
                              placeholder="请选择生产日期" style="width:100%"/>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="有效期" prop="sample_info.validity_date">
              <el-date-picker v-model="form.sample_info.validity_date" type="date"
                              :picker-options="pickerOption1" value-format="yyyy-MM-dd" @change="onChangeTime12"
                              placeholder="请选择有效期" style="width:100%"/>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="经销商">
              <el-input v-model="form.sample_info.supplier" placeholder="请输入经销商"/>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="生产厂家">
              <el-input v-model="form.sample_info.producer" placeholder="请输入生产厂家"/>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-row :gutter="10">
              <el-col :span="12">
                <el-form-item label="送检量" prop="sample_num">
                  <el-input v-if="form.total_count === 0" v-model="form.sample_num" @change="handlSampleNum"
                            placeholder="请输入数量"/>
                  <el-input v-else v-model="form.sample_num" @change="handlSampleNum"
                            placeholder="请输入数量">
                    <template #append>
                      <span>库存：{{form.total_count}}</span>
                    </template>
                  </el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="单位" prop="sample_info.unit">
                  <el-input v-model.trim="form.sample_info.unit" placeholder="请输入单位"/>
                </el-form-item>
              </el-col>
            </el-row>
          </el-col>


          <el-col :span="12">
            <el-row :gutter="10">
              <el-col :span="12">
                <el-form-item label="批量" prop="batch_num">
                  <el-input v-model="form.batch_num" @change="handlBatch"
                            placeholder="请输入数量"/>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="单位" prop="batch_unit">
                  <el-input v-model.trim="form.batch_unit" placeholder="请输入单位"/>
                </el-form-item>
              </el-col>
            </el-row>
          </el-col>


          <el-col :span="12">
            <el-form-item label="是否留样" :required="isHaveWho" prop="storage_conditions">
              <el-input v-model="form.storage_conditions" :disabled="form.if_reserve_sample ===2"
                        placeholder="请输入贮存条件">
                <template slot="prepend">
                  <el-radio-group v-model="form.if_reserve_sample" @change="onChangeRadio">
                    <el-radio :label="1">是</el-radio>
                    <el-radio :label="2">否</el-radio>
                  </el-radio-group>
                  <span style="padding-left:10px">贮存条件</span>
                </template>
              </el-input>
            </el-form-item>
          </el-col>


          <!--          <el-col :span="12">-->
          <!--            <el-form-item label="来源">-->
          <!--              <el-input v-model="form.source" disabled/>-->
          <!--            </el-form-item>-->
          <!--          </el-col>-->

          <!--          <el-col :span="12">-->
          <!--            <el-form-item label="实验目的">-->
          <!--              <el-input v-model="form.experiment_goal" placeholder="实验目的"/>-->
          <!--            </el-form-item>-->
          <!--          </el-col>-->

          <!-- <el-col :span="12">
            <el-form-item label="检验标准" prop="inspection_standard">
              <el-input v-model="form.inspection_standard" placeholder="请输入检验标准">
                <el-checkbox slot="append" v-model="form.if_qs_coa" :true-label="1" :false-label="2"
                  label="厂家QS及COA"></el-checkbox>
              </el-input>
            </el-form-item>
          </el-col> -->

          <el-col :span="12">
            <el-form-item label="检验期限">
              <el-date-picker v-model="form.inspection_date" type="date" value-format="yyyy-MM-dd"
                              :picker-options="pickerOption1" @change="onChangeTime2" placeholder="请选择检验期限"
                              style="width:100%"/>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="检验依据" :required="ishandlQuality" prop="quality_standard_file">
              <el-row :gutter="20">
                <el-col :span="12">
                  <el-radio-group v-model="form.quality_standard_type" @change="handlRadio">
                    <el-radio :label="1">选择已有检验依据</el-radio>
                    <el-radio :label="2">上传新检验依据</el-radio>
                  </el-radio-group>
                </el-col>
                <el-col :span="12" v-if="form.quality_standard_type === 1">
                  <el-row>
                    <el-col :span="16">
                      <el-select v-model="form.quality_standard_id" style="width:100%"
                                 @change="onChangeQuality" placeholder="请选择检验依据">
                        <el-option v-for="item in projectList" :key="item.id"
                                   :label="item.quality_standard_name" :value="item.id"/>
                      </el-select>
                    </el-col>
                    <el-col :span="8" style="text-align: right">
                      <el-dropdown>
                        <span class="el-dropdown-link">
                          查看检验依据<i class="el-icon-arrow-down el-icon--right"></i>
                        </span>
                        <el-dropdown-menu slot="dropdown">
                          <div v-if="fileList.length === 0"
                               style="width: 200px;padding:10px 5px;font-size: 12px"> 暂无
                          </div>
                          <div v-else style="width: 200px">
                            <el-dropdown-item v-for="item in fileList" :key="item.id"
                                              @click.native="openFile(item)"
                                              style="cursor: pointer;color: #409eff;">
                              {{item.old_name}}{{item.suffix}}
                            </el-dropdown-item>
                          </div>
                        </el-dropdown-menu>
                      </el-dropdown>
                    </el-col>
                  </el-row>

                </el-col>
                <el-col :span="12" v-else-if="form.quality_standard_type ===2">
                  <el-row>
                    <el-col :span="12">
                      <upload ref="upload" :showList="false" @uploadSuccess="uploadSuccess11"
                              :tipShow="false" btnTitle="上传检验依据"/>
                    </el-col>
                    <el-col :span="12">
                      <el-dropdown v-if="form.quality_standard_file.length > 0">
                        <span class="el-dropdown-link">
                          查看检验依据<i class="el-icon-arrow-down el-icon--right"></i>
                        </span>
                        <el-dropdown-menu slot="dropdown">
                          <el-dropdown-item v-for="item in form.quality_standard_file"
                                            :key="item.id">
                            <div
                              style="display: flex;align-items: center;justify-content: space-between;width: 200px">
                              <div @click="openFile1(item)" style="cursor: pointer;color: #409eff;">
                                {{item.old_name}}{{item.suffix}}
                              </div>

                              <el-button type="text" style="color:red"
                                         @click.native.stop="handlDele(item)" icon="el-icon-delete">
                              </el-button>

                            </div>
                          </el-dropdown-item>
                        </el-dropdown-menu>
                      </el-dropdown>
                    </el-col>
                  </el-row>
                </el-col>
              </el-row>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="检验项目">
              <el-input v-model="form.inspection_project" placeholder="请输入检验项目"/>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="是否返样">
              <el-radio-group v-model="form.if_return_sample">
                <el-radio :label="0">否</el-radio>
                <el-radio :label="1">是</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>


          <el-col :span="24">
            <el-form-item label="备注">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入备注"/>
            </el-form-item>
          </el-col>

          <el-col :span="24">
            <el-form-item label="厂检报告" >
              <upload-select ref="uploadEnclosure1" :edit-list="form.factory_inspection_report"
                             @selectSuccess="selectSuccess"
                             @uploadSuccess="uploadSuccess"
                             @deleteSuccess="deleteSuccess"/>
            </el-form-item>
          </el-col>

          <el-col :span="24">
            <el-form-item label="合成路线">
              <upload-select ref="uploadEnclosure2" :edit-list="form.synthetic_route" @uploadSuccess="uploadSuccess"
                             @deleteSuccess="deleteSuccess"/>
            </el-form-item>
          </el-col>

          <el-col :span="24">
            <el-form-item label="附件">
              <upload-select ref="uploadEnclosure" :edit-list="form.enclosure" @uploadSuccess="uploadSuccess"
                             @deleteSuccess="deleteSuccess"/>
            </el-form-item>
          </el-col>

        </el-row>
      </el-form>
    </div>
    <!--对话框 -->
    <el-dialog title="上传质量标准" :visible.sync="open" width="600px" append-to-body
               :close-on-click-modal="false">
      <upload ref="upload" :showList="false"/>
      <div slot="footer" class="dialog-footer">
        <el-button plain size="small" @click="open = false">取 消</el-button>
        <el-button size="small" type="primary" @click="submitForm">确定</el-button>
      </div>
    </el-dialog>

  </div>

</template>

<script>
  import {
    apply,
    projectNamesss,
    quality,
    viewQuality,
    applyGoodsrSample,
    detail,
    applyEditor,
    lookInfoSet,
    sampleType
  } from '@/api/sampleInspection'
  import { filePreviewFun, fileDeleteFunc } from '@/utils/file'

  export default {
    data() {
      let CheckNum = (rule, value, callback) => {
        const r = /(^$)|^[0-9]+(.[0-9]{1,3})?$/ //保留三位小数
        if (value === '') {
          callback(new Error('输入不能为空'))
        } else if (!r.test(value)) {
          callback(new Error('只能输入整数或者保留三位小数'))
        } else {
          callback()
        }
      }
      let CheckNumm = (rule, value, callback) => {
        //const rr = /^\+?[1-9][0-9]*$/ // 正整数
        const rr = /(^$)|^[0-9]+(.[0-9]{1,3})?$///保留一位小数
        if (value === '') {
          callback(new Error('输入不能为空'))
        } else if (!rr.test(value)) {
          callback(new Error('只能输入整数或者保留三位小数'))
        } else {
          if (this.form.total_count === 0) {
            callback()
          } else {
            if (value > this.form.total_count) {
              callback(new Error('送检量大于库存'))
            } else {
              callback()
            }
          }
        }
      }
      let checkStorage = (rule, value, callback) => {
        if (this.form.if_reserve_sample === 1) {
          if (value === '') {
            callback(new Error('请输入贮存条件'))
          } else {
            callback()
          }
        } else {
          callback()
        }
      }
      let checkHandl = (rule, value, callback) => {
        if (this.form.inspection_goal === '复检') {
          callback()
        } else {
          if (this.form.quality_standard_type === 1) {
            if (this.form.quality_standard_id === '') {

              callback(new Error('请选择检验依据'))
            } else {
              callback()
            }
          } else if (this.form.quality_standard_type === 2) {
            if (this.form.quality_standard_file.length === 0) {
              callback(new Error('请上传检验依据'))
            } else {
              callback()
            }
          } else {
            callback()
          }
        }
      }
      let checkFiles = (rule, value, callback) => {
        console.log(this.objData.fileList.length, this.objData.newFileList.length)
        if (this.objData.fileList.length > 0 || this.objData.newFileList.length > 0) {
          callback()
        } else {
          callback(new Error('请上传厂检报告'))
        }

      }
      return {
        objData: {
          fileList: [],
          newFileList: []
        },
        radio: 1,
        sampleTypeList: [],
        form: {
          batch_num: null,
          batch_unit: '',
          quality_standard_type: 1,
          name: '',
          inspection_project: '',
          source: '自制', //来源
          experiment_goal: '', //实验目的
          inspection_goal_id: '',
          inspection_type: 'inspection_type_wg', //	请验类型  1 外购物料 2 自研产品
          inspection_type_name: '外购物料',
          project_name: '', //项目名称
          project_id: '', //项目id
          project_leader_name: '',
          project_leader_id: '',
          project_phases_id: '',
          sample_info: {
            validity_date: '',
            sample_type_info: {
              same_type_id: '',
              same_type_name: ''
            },
            unit: '',
            id: '',
            name: '',
            attr: '', //规格
            cas: '', //CAS 1
            supplier: '', //经销商1
            producer: '', //生产商1
            batch_number: '', //批号1
            production_date: '', //生产日期1
            sample_type_name: '', //检品名称
            sample_type_id: '' //检品id
          },
          quality_standard_file: [], //质量文件
          sample_num: null, //送检量
          inspection_goal: '', //检验目的
          project_no: '', //项目编号
          if_reserve_sample: 1, //是否留样   1 是  2 否
          storage_conditions: '', //储存条件
          inspection_date: '', //检验日期
          // inspection_standard: "", //检验标准
          // if_qs_coa: 1, //是否有厂家的QS及COA  1 是  2 否
          quality_standard_name: '', //检验项目名称
          quality_standard_id: '', //检验项目id
          remark: '', //备注
          enclosure: [], //附件,
          project_category_id: '', //项目类别id
          project_category_name: '', //项目类别
          sign_category_id: '', //申报类别id
          sign_category_name: '', //申报类别
          synthetic_route: [],//   合成路线
          factory_inspection_report: [],// 厂检报告
          if_return_sample: 0,//是否反样  0 否 1 是   int
          total_count: 0
        },
        rules: {
          inspection_goal_id: [{ required: true, message: '请选择检验目的', trigger: 'change' }],
          'sample_info.name': [
            { required: true, message: '请输入检品名称', trigger: 'blur' }
          ],
          'sample_info.unit': [
            { required: true, message: '请输入送检量单位', trigger: 'blur' }
          ],
          batch_unit: [
            { required: true, message: '请输入批量单位', trigger: 'blur' }
          ],
          project_name: [{ required: true, message: '请选择关联项目', trigger: 'change' }],
          sample_num: [{ required: true, validator: CheckNumm, trigger: 'blur' }],
          batch_num: [{ required: true, validator: CheckNum, trigger: 'blur' }],
          storage_conditions: [{ validator: checkStorage, trigger: 'change' }],
          quality_standard_file: [{ validator: checkHandl, trigger: 'change' }],
          'sample_info.validity_date': [{ required: true, message: '请选择有效期', trigger: 'change' }],
          // factory_inspection_report: [{ required: true, validator: checkFiles, trigger: 'change' }],
          'sample_info.batch_number': [{ required: true, message: '请输入批号', trigger: 'blur' }]
        },
        projectList: [],
        projectData: [],
        fileList: [],
        fileListData: [],
        open: false,
        pickerOption: {
          disabledDate(time) {
            //此条为设置禁止用户选择今天之前的日期，包含今天。
            return time.getTime() >= Date.now() - 8.64e7
          }
        },
        pickerOption1: {
          disabledDate(time) {
            //此条为设置禁止用户选择今天之前的日期，包含今天。
            return time.getTime() <= Date.now() - 8.64e7
          }
        },
        inputList: [],
        demandSourceOptions: [],
        testingPurpose: [],
        titleName: '创建送检申请'
      }
    },
    //是计算属性，也可以理解为一个方法。
    computed: {
      isHaveWho: function() {
        return this.form.if_reserve_sample === 1
      },

      ishandlQuality: function() {
        return this.form.inspection_goal !== '复检'
      }
    },

    //属性是手动定义的所需监听的值
    watch: {
      form: {
        handler(newVal) {
          if (newVal) {
            if (newVal.factory_inspection_report.length > 0) {
              this.$nextTick(_ => {
                // this.$refs.form.clearValidate('factory_inspection_report')
              })
            }
          }
        },
        deep: true,
        immediate: true
      }
    },

    //生命周期 - 创建完成（访问当前this实例）
    created() {
      this.handleQuality()
      this.remoteMethod()
      if (this.$route.query.targetType) {
        let aaa = localStorage.getItem('sampleInspection')
        this.form = JSON.parse(aaa)
        if(this.form.total_count !== 0){
          this.form.batch_num = this.form.total_count
          this.form.batch_unit = this.form.sample_info.unit
        }
        if (this.form.inspection_type === 'inspection_type_wg') {
          this.form.inspection_type_name = '外购物料'
        }
        if (this.form.inspection_type === 'inspection_type_zy') {
          this.form.inspection_type_name = '自研产品'
        }
      }
      if (this.$route.query.s_id) {
        this.detailData()
      }
      this.getSysAllDic()
    },

    //生命周期 - 挂载完成（访问DOM元素）
    mounted() {
      lookInfoSet({ dictType: 'sample_type' }).then(res => {
        if (res.code === 200) {
          this.sampleTypeList = res.data || []
        }
      })

      lookInfoSet({ dictType: 'inspection_goal' }).then(res => {
        if (res.code === 200) {
          this.testingPurpose = res.data || []
        }
      })
    },

    //是Vue实例对象上绑定的方法，供当前Vue组件作用域内使用，
    methods: {
      //样品类型事件
      handlSampType(val) {
        this.form.sample_info.sample_type_info.same_type_id = val
        let index = this.sampleTypeList.findIndex(a => a.dict_key === val)
        if (index !== -1) {
          this.form.sample_info.sample_type_info.same_type_name = this.sampleTypeList[index].dict_val
        }
      },

      //检验目的事件
      handlGoal(val) {
        this.form.inspection_goal_id = val
        let index = this.testingPurpose.findIndex(a => a.dict_key === val)
        if (index !== -1) {
          this.form.inspection_goal = this.testingPurpose[index].dict_val
        }
      },

      //编辑
      detailData() {
        detail(this.$route.query.s_id).then(res => {
          if (res.code === 200) {
            this.form = res.data || []
            this.objData.fileList = res.data.factory_inspection_report
            console.log(this.form, '-=-=-==-')
            if (this.form.inspection_status == 6) {
              this.titleName = '编辑送检申请'
            }
            this.onChangeQuality(this.form.quality_standard_id)
            this.fileListData = res.data.enclosure || []
          }
        })
      },

      //
      getSysAllDic() {
        lookInfoSet({ dictType: 'inspection_type' }).then(res => {
          if (res.code === 200) {
            this.demandSourceOptions = res.data
            let index = this.demandSourceOptions.findIndex(
              a => a.dict_key === 'inspection_type_wd'
            )
            if (index !== -1) {
              this.demandSourceOptions.splice(index, 1)
            }
          }
        })
      },

      //检品名称
      async querySearchAsync(queryString, cb) {
        let data = {
          key: queryString
        }
        let res = await applyGoodsrSample(data)
        if (res.code === 200) {
          this.inputList = res.data
        } else {
          this.inputList = []
        }
        let results = this.inputList
        cb(results)
      },

      handlInspection(val) {
        if (val) {
          this.form.sample_info = val
          this.form.sample_info.sample_type_info = {
            same_type_id: '',
            same_type_name: ''
          }
        }
        if (this.form.sample_info.name) {
          this.$nextTick(_ => {
            this.$refs.form.clearValidate('sample_info.name')
          })
        }
      },

      //项目名称下拉
      handleSelect(item) {
        this.form.project_id = item.id
        this.form.project_no = item.project_no
        this.form.project_name = item.project_name
        this.form.project_leader_name = item.project_person_in_charge
        this.form.project_leader_id = item.project_person_in_charge_id
        if (this.form.project_no || this.form.project_no !== '') {
          this.$refs.form.fields.map(i => {
            if (i.prop === 'project_no') {
              i.clearValidate()
            }
          })
        }
      },

      //获取项目名称
      async remoteMethod(queryString) {
        let res = await projectNamesss()
        if (res.code === 200) {
          this.projectData = res.data.data || []
        }
      },

      //项目名称下拉
      handlChange(val) {
        if (val) {
          this.projectData.forEach(item => {
            if (val === item.id) {
              this.form.project_id = item.id
              this.form.project_no = item.project_no
              this.form.project_name = item.project_name
              this.form.project_leader_name = item.project_person_in_charge
              this.form.project_leader_id = item.project_person_in_charge_id
              this.form.project_category_id = item.project_category_id
              this.form.project_category_name = item.project_category_name
              this.form.sign_category_id = item.sign_category_id
              this.form.sign_category_name = item.sign_category_name
              this.form.project_phases_id = item.project_phases_id || ''
            }
          })
        } else {
          this.form.project_id = ''
          this.form.project_no = ''
          this.form.project_name = ''
          this.form.project_leader_name = ''
          this.form.project_leader_id = ''
          this.form.project_category_id = ''
          this.form.project_category_name = ''
        }
      },

      //质量标准事件
      handlRadio(val) {
        if (val == 1 || val == 2) {
          this.form.quality_standard_file = []
          this.fileList = []
          this.form.quality_standard_id = ''
        }
      },

      //获取质量标准
      handleQuality() {
        let data = {
          condition: {
            quality_type_name: '',
            quality_source_name: '',
            quality_standard_name: '',
            quality_standard_status: 4
          },
          pager: {
            skip: 1,
            limit: 30
          }
        }
        quality(data).then(res => {
          if (res.code === 200) {
            this.projectList = res.data.data || []
          }
        })
      },

      //时间事件
      onChangeTime1(val) {
        if (val) {
          this.form.sample_info.production_date = `${val} 00:00:00`
        }
      },

      onChangeTime12(val) {
        if (val) {
          this.form.sample_info.validity_date = `${val} 00:00:00`
        }
      },

      onChangeTime2(val) {
        this.form.inspection_date = `${val} 00:00:00`
      },

      //留样单选
      onChangeRadio(val) {
        this.form.if_reserve_sample = val
        if (val === 2) {
          this.form.storage_conditions = ''
        }
      },

      //质量标准事件
      onChangeQuality(val) {
        this.form.quality_standard_id = val
        this.projectList.forEach(item => {
          if (item.id === val) {
            this.quality_standard_name = item.quality_standard_name
            this.form.quality_standard_file = item.file || []
            this.fileList = item.file || []
            this.$nextTick(_ => {
              this.$refs.form.clearValidate('quality_standard_file')
            })
          }

        })
        // viewQuality(val).then(res => {
        //   if (res.code === 200) {
        //     this.fileList = res.data.file || []
        //     this.form.quality_standard_file = res.data.file || []
        //     if (this.form.quality_standard_file.length > 0) {
        //       this.$nextTick(_ => {
        //         this.$refs.form.clearValidate('quality_standard_file')
        //       })
        //     }
        //   }
        // })
      },

      //查看质量标准
      openFile(file) {
        filePreviewFun(file)
      },

      //查看质量标准
      openFile1(file) {
        filePreviewFun(file)
      },

      //删除质量标准
      handlDele(file) {
        fileDeleteFunc({ id: file.id })
          .then(_ => {
            let index = this.form.quality_standard_file.findIndex(
              a => a.id === file.id
            )
            if (index !== -1) {
              this.form.quality_standard_file.splice(index, 1)
            }
          })
          .catch(() => {
            this.$message({
              type: 'error',
              message: '删除失败'
            })
          })
      },

      //上传质量标准
      uploadFile() {
        this.open = true
      },

      //送检量事件
      handlSampleNum(val) {
        this.form.sample_num = Number(val)
      },

      handlBatch(val) {
        this.form.batch_num = Number(val)
      },

      //提交
      submitForm() {
        if (this.$refs.upload.emitFileList().length === 0) {
          this.$message({
            message: '请上传质量标准',
            type: 'error'
          })
          return
        }
        this.form.quality_standard_file = this.$refs.upload.emitFileList()
        this.open = false
      },

      //提交
      handleSubmit() {
        let _self = this
        let data = {
          targetType: this.$route.query.targetType || '0',
          targetId: this.$route.query.targetId || '0'
        }
        _self.$refs.form.validate(async valid => {
          if (valid) {
            let dataList = await this.$refs.uploadEnclosure.saveFile()
            let dataList1 = await this.$refs.uploadEnclosure1.saveFile()
            let dataList2 = await this.$refs.uploadEnclosure2.saveFile()
            _self.form.enclosure = dataList[0]
            _self.form.factory_inspection_report = dataList1[0]
            _self.form.synthetic_route = dataList2[0]
            let msg = ''
            if (_self.$route.query.s_id) {
              _self.form.id = _self.$route.query.s_id
              msg = '修改成功'
              applyEditor(_self.form).then(res => {
                if (res.code === 200) {
                  _self.$message.success(msg)
                  _self.$route.params.refresh = true
                  _self.$router.push('/sampleInspection/applied')
                } else {
                  _self.$message.error(res.msg)
                }
              })
            } else {
              msg = '添加成功'
              apply(data, _self.form).then(res => {
                if (res.code === 200) {
                  _self.$message.success(msg)
                  _self.$route.params.refresh = true
                  _self.$router.push('/sampleInspection/applied')
                } else {
                  _self.$message.error(res.msg)
                }
              })
            }
          } else {
            return false
          }
        })
      },

      //返回
      handleBack() {
        this.$router.go(-1)
      },

      //重置
      handleReset() {
        this.$nextTick(_ => {
          this.$refs.form.clearValidate()
        })
      },

      //
      handlChangeType(val) {
        this.handleReset()
        this.form.inspection_type = val
        if (val) {
          let index = this.demandSourceOptions.findIndex(a => a.dict_key == val)
          if (index !== -1) {
            this.form.inspection_type_name = this.demandSourceOptions[index].dict_val
          }
        }

      },
      selectSuccess(obj) {
        console.log(obj, '-------')
        this.objData = obj
        if (obj.fileList.length > 0 || obj.newFileList.length > 0) {
          this.$nextTick(_ => {
            // this.$refs.form.clearValidate('factory_inspection_report')
          })
        }
      },
      //删除
      deleteSuccess() {
        // this.tableFormData.enclosure = this.$refs.upload.emitFileList()
      },

      //预览
      uploadSuccess() {
      },

      uploadSuccess11() {
        this.form.quality_standard_file = this.$refs.upload.emitFileList()
        if (this.form.quality_standard_file.length > 0) {
          this.$nextTick(_ => {
            this.$refs.form.clearValidate('quality_standard_file')
          })
        }
        console.log(this.form.quality_standard_file, '---------')
      }
    }
  }
</script>
<style scoped>
  /* @import url(); 引入css类 */
  ::v-deep .el-input-group__prepend {
    background-color: #ffffff;
    height: 30px;
    box-sizing: border-box;
  }

  ::v-deep .el-input-number,
  .el-input-number--small {
    border-bottom: 0;
    border-top: 0;
  }

  ::v-deep .el-input-number .el-input__inner {
    border-bottom: 0;
    border-top: 0;
    height: 30px;
    box-sizing: border-box;
  }

  ::v-deep .el-input-group__append {
    background-color: #ffffff !important;
  }

  .el-row {
    display: flex;
    justify-content: flex-start;
    flex-flow: wrap;
  }

  .el-dropdown-link {
    cursor: pointer;
    color: #409eff;
  }

  .el-icon-arrow-down {
    font-size: 12px;
  }
</style>
